package com.cspm.ahjy.bc.office.word.util;

import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.util.StringUtil;
import com.cspm.ahjy.bc.office.util.UnitConvertUtil;
import com.cspm.ahjy.bc.office.word.domain.MSWordartStyle;

/**
 * 艺术字样式工具类
 * 
 * @author ServerZhang
 * @date 2017年10月19日
 */
public class WordartStyleUtil {

	/**
	 * 解析shape标签到艺术字的样式对象MSWordartStyle
	 * @param shapeElement
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月19日
	 */
	public static MSWordartStyle getMsWordartStyle(Element shapeElement) {
		MSWordartStyle wordartStyle = new MSWordartStyle();
		
		Attribute styleA = shapeElement.attribute("style");
		if (styleA != null) {
			String text = styleA.getText();
			Map<String, String> map = StringUtil.toMap(text);
			String width = map.get("width");
			if (!StringUtils.isEmpty(width)) {
				Integer standard = UnitConvertUtil.convert2Standard(width);
				wordartStyle.setWidth(standard);
			}
			String height = map.get("height");
			if (!StringUtils.isEmpty(height)) {
				Integer standard = UnitConvertUtil.convert2Standard(height);
				wordartStyle.setHeight(standard);
			}
			String rotation = map.get("rotation");
			if (!StringUtils.isEmpty(rotation)) {
				wordartStyle.setRotation(Integer.parseInt(rotation));
			}
		}
		
		Attribute typeA = shapeElement.attribute("type");
		if (typeA != null) {
			wordartStyle.setType(typeA.getText());
		}
		
		Attribute fillcolorA = shapeElement.attribute("fillcolor");
		if (fillcolorA != null) {
			wordartStyle.setFillcolor(fillcolorA.getText());
		}
		
		Element shadowE = shapeElement.element("shadow");
		if (shadowE != null) {
			Attribute color = shadowE.attribute("color");
			if (color != null) {
				wordartStyle.setShadowColor(color.getText());
			}
		}
		
		Element extrusionE = shapeElement.element("extrusion");
		if (extrusionE != null) {
			Attribute color = extrusionE.attribute("color");
			if (color != null) {
				wordartStyle.setExtrusionColor(color.getText());
			}
		}
		
		String type = wordartStyle.getType();
		String shadowColor = wordartStyle.getShadowColor();
		String fillcolor = wordartStyle.getFillcolor();
		String extrusionColor = wordartStyle.getExtrusionColor();
		Integer rotation = wordartStyle.getRotation();

		String styleNo = null;
		String location = null;
		switch (type) {
		case "#_x0000_t136":
			if (fillcolor != null) {
				switch (fillcolor) {
				case "#369":
					styleNo = "22";
					location = "第2行第2列";
					break;
				case "#b2b2b2":
					styleNo = "23";
					location = "第2行第3列";
					break;
				case "#06c":
					styleNo = "25";
					location = "第2行第5列";
					break;
				case "maroon":
					styleNo = "26";
					location = "第2行第6列";
					break;
				case "yellow":
					styleNo = "31";
					location = "第3行第1列";
					break;
				case "#063":
					styleNo = "33";
					location = "第3行第3列";
					break;
				case "#9400ed":
					styleNo = "34";
					location = "第3行第4列";
					break;
				case "#c4b596":
					styleNo = "36";
					location = "第3行第6列";
					break;
				case "#fc9":
					styleNo = "41";
					location = "第4行第1列";
					break;
				case "#dcebf5":
					styleNo = "55";
					location = "第5行第5列";
					break;
				case "#c00":
					styleNo = "56";
					location = "第5行第6列";
					break;
				case "black":
					styleNo = "16";
					location = "第1行第6列";
					break;

				default:
					break;
				}
			}else {
				if ("white".equals(extrusionColor)) {
					styleNo = "51";
					location = "第5行第1列";
				}else {
					if (shadowColor != null) {
						switch (shadowColor) {
						case "#4d4d4d":
							styleNo = "24";
							location = "第2行第4列";
							break;
						case "#868686":
							if (rotation != null && rotation.equals(90)) {
								styleNo = "16";
								location = "第1行第6列";
							}else {
								styleNo = "11";
								location = "第1行第1列";
							}
							break;
						default:
							break;
						}
					}else {
						styleNo = "21";
						location = "第2行第1列";
					}
				}
			}
			break;
		case "#_x0000_t172":
			if ("#60c".equals(fillcolor)) {
				styleNo = "32";
				location = "第3行第2列";
			}else {
				styleNo = "12";
				location = "第1行第2列";
			}
			break;
		case "#_x0000_t144":
			styleNo = "13";
			location = "第1行第3列";
			break;
		case "#_x0000_t161":
			styleNo = "14";
			location = "第1行第4列";
			break;
		case "#_x0000_t175":
			styleNo = "15";
			location = "第1行第5列";
			break;
		case "#_x0000_t156":
			styleNo = "35";
			location = "第3行第5列";
			break;
		case "#_x0000_t138":
			styleNo = "42";
			location = "第4行第2列";
			break;
		case "#_x0000_t170":
			styleNo = "43";
			location = "第4行第3列";
			break;
		case "#_x0000_t158":
			styleNo = "44";
			location = "第4行第4列";
			break;
		case "#_x0000_t152":
			styleNo = "45";
			location = "第4行第5列";
			break;
		case "#_x0000_t159":
			styleNo = "46";
			location = "第4行第6列";
			break;
		case "#_x0000_t154":
			if ("#060".equals(fillcolor)) {
				styleNo = "52";
				location = "第5行第2列";
			}else {
				styleNo = "54";
				location = "第5行第4列";
			}
			break;
		case "#_x0000_t163":
			styleNo = "53";
			location = "第5行第3列";
			break;

		default:
			break;
		}
		
		wordartStyle.setStyleNo(styleNo);
		wordartStyle.setLocation(location);
		
		return wordartStyle;
	}
}
